﻿using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace csLineCounter
{
    class Program
    {
        private static int _files = 0;
        private static int _spaceLines = 0;
        private static int _bracketLines = 0;
        private static int _ifLines = 0;
        private static int _otherLines = 0;
        
        static void Main(string[] args)
        {
            var startDir = @"D:\temp\CLAVIRE\";// (args.Length > 0) ? args[0] : ".";
            ProcessDir(startDir);
            Console.WriteLine("Files: {0}\nSpaceLines: {1}\nBracket lines: {2}\nIF lines: {3}\nOther lines: {4}", _files, _spaceLines, _bracketLines, _ifLines, _otherLines);
        }

        private static void ProcessDir(string startDir)
        {
            var di = new DirectoryInfo(startDir);
            int localSpaceLines = 0;
            int localBracketLines = 0;
            int localIfLines = 0;
            int localOtherLines = 0;
            var fileList = di.GetFiles("*.cs").Concat(di.GetFiles("*.php")).Concat(di.GetFiles("*.java"));

            foreach (var file in fileList)
            {
                _files++;
                Console.WriteLine("{0}", file.FullName);
                var content = File.ReadAllText(file.FullName);
                content = Regex.Replace(content, @"/\*.*\*/", "");
                content = Regex.Replace(content, @"//.*$", "", RegexOptions.Multiline);
                var lines = content.Split(new char[] {'\n', '\r'}, StringSplitOptions.RemoveEmptyEntries);
                foreach (var line in lines)
                {
                    int spaceCounter = 0;
                    int bracketCounter = 0;
                    for (int i = 0; i < line.Length; i++)
                    {
                        if ((line[i] == ' ') || (line[i] == '\t') || (line[i] == '\n') || (line[i] == '\r'))
                            spaceCounter++;
                        if ((line[i] == '{') || line[i] == '}')
                            bracketCounter++;
                    }
                    if ((spaceCounter == line.Length) || ((line[0] == '/') && ((line[1] == '/') || (line[1] == '*'))))
                        localSpaceLines++;
                    else if ((spaceCounter == (line.Length - 1)) && (bracketCounter == 1))
                        localBracketLines++;
                    else if (Regex.Match(line, @"[\W^]if[\W$]").Success)
                        localIfLines++;
                    else
                        localOtherLines++;
                }
                Console.WriteLine("s:{0}\tb:{1}\ti:{2}\to:{3}", localSpaceLines, localBracketLines, localIfLines, localOtherLines);
            }
            _spaceLines += localSpaceLines;
            _bracketLines += localBracketLines;
            _ifLines += localIfLines;
            _otherLines += localOtherLines;
            foreach (var directory in di.GetDirectories())
                ProcessDir(directory.FullName);
        }
    }
}
